<script setup>
import { ref, onMounted } from 'vue'
import { getChannelList, artDelChannelService } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'

const loading = ref(false)
const channelList = ref([])
const dialog = ref()

const getList = async () => {
  loading.value = true
  const res = await getChannelList()
  console.log(res.data.data)
  channelList.value = res.data.data
  loading.value = false
}
onMounted(() => {
  getList()
})

// 添加
const onAddChannel = () => {
  dialog.value.open({})
}
//编辑
const onEditChannel = (row) => {
  // console.log('1111', row)
  dialog.value.open(row)
}
const onDelChannel = async (row) => {
  await artDelChannelService(row.id)
  ElMessage.success('删除成功')
  getList()
}

//监听子组件事件
const onSuccess = () => {
  getList()
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel"> 添加分类 </el-button>
    </template>

    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column type="index" width="100" label="序号" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            type="primary"
            plain
            circle
            :icon="Edit"
            @click="onEditChannel(row)"
          ></el-button>
          <el-button
            type="danger"
            plain
            circle
            :icon="Delete"
            @click="onDelChannel(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 弹框 -->
    <ChannelEdit ref="dialog" @success="onSuccess"></ChannelEdit>
  </page-container>
</template>
